$(function() {
  var couponid = getSearch().couponId
  var couponTitle = getSearch().couponTitle
  couponTitle = decodeURI(couponTitle)
  $('.couponTitle').text(couponTitle)
  var length = 0
  $.ajax({
    url:'http://127.0.0.1:9090/api/getcouponproduct',
    dataType: 'json',
    data: {
      couponid: couponid
    },
    success: function(info) {
      console.log(info)
      $('.mmb_navList .content').html(template('tmp',info))
      length = info.result.length
    }
  })

  var index;
  $('.mmb_navList').on('click','.item',function() {
    $('.couponModal').show();
    var src = $(this).find('img').attr('src')
    $('.couponModal img').attr('src',src)
    
    index = $(this).data('index')
    console.log(index)
  })
  $('.couponModal .left').on('click',function() {
    console.log(index)
    if(index > 0) {
      index--
    }else {
      return
    }
    var src = $('.mmb_navList .item[data-index=' + index + ']').find('img').attr('src')
    console.log(src)
    $('.couponModal img').attr('src',src)
  })

  $('.couponModal .right').on('click',function() {
    console.log(index)
    if(index < length - 1) {
      index++
    }else {
      return
    }
    var src = $('.mmb_navList .item[data-index=' + index + ']').find('img').attr('src')
    console.log(src)
    $('.couponModal img').attr('src',src)
  })
  var startX;
  $('.couponModal .imgBox img').on('touchstart',function(e) {
    startX = e.originalEvent.touches[0].clientX;
  })
  $('.couponModal .imgBox img').on('touchend',function(e) {
    var endX = e.originalEvent.changedTouches[0].clientX;
    console.log(endX - startX)
    if(endX - startX > 50) {
      $('.couponModal .left').trigger('click')
    }
    if(endX - startX < -50) {
      $('.couponModal .right').trigger('click')
    }
  })

  $('.couponModal .over').on('click',function() {
    $('.couponModal').hide()
  })
})